package es.dk.hl.commands
{
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.facebook.commands.batch.BatchRun;
	import com.facebook.commands.data.DeleteObject;
	import com.facebook.commands.data.RemoveAssociation;
	import com.facebook.data.batch.BatchCollection;
	import com.facebook.events.FacebookEvent;
	
	import es.dk.hl.event.FBEvent;
	import es.dk.hl.model.HomeLinkModelLocator;

	public class FaceBook_Delete_Command implements ICommand
	{
		
		public function FaceBook_Delete_Command()
		{
		}
		
		public function execute(event:CairngormEvent):void {
			var batch:BatchCollection = new BatchCollection();
			batch.addItem(
				new RemoveAssociation(
					"usercompo",
					Number(HomeLinkModelLocator.getInstance().facebookUser.uid), 
					HomeLinkModelLocator.getInstance().composicion_id));
			var batchRun:BatchRun = HomeLinkModelLocator.getInstance().facebook.post(new BatchRun(batch)) as BatchRun;
			batchRun.addEventListener(FacebookEvent.COMPLETE, onBatchComplete1);
		}

		protected function onBatchComplete1(event:FacebookEvent):void {
			if (event.success) {
				var batch:BatchCollection = new BatchCollection();
				batch.addItem(
					new DeleteObject(
						String(HomeLinkModelLocator.getInstance().composicion_id)));
				var batchRun:BatchRun = HomeLinkModelLocator.getInstance().facebook.post(new BatchRun(batch)) as BatchRun;
				batchRun.addEventListener(FacebookEvent.COMPLETE, onBatchComplete2);
			}
		}
		
		protected function onBatchComplete2(event:FacebookEvent):void {
			if (event.success) {
				new  FBEvent(FBEvent.FBEvent_CREATEANDSET).dispatch();
			}
		}	

	}
}